package cn.enited.devtools.apiaggregator.controller;

import cn.enited.devtools.apiaggregator.dto.*;
import cn.enited.devtools.apiaggregator.service.ProjectService;
import cn.enited.devtools.apiaggregator.utils.ActionUtil;
import cn.enited.devtools.apiaggregator.validator.group.CreateValidationGroup;
import cn.enited.devtools.apiaggregator.validator.group.FindValidationGroup;
import cn.enited.devtools.apiaggregator.validator.group.PagingValidationGroup;
import cn.enited.devtools.apiaggregator.validator.group.UpdateValidationGroup;
import cn.enited.devtools.apiaggregator.vo.ProjectParamVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/project")
public class ProjectController extends BaseController {

    private ProjectService projectService;

    @Autowired
    public void setProjectService(ProjectService projectService) {
        this.projectService = projectService;
    }

    @GetMapping(path = "/get/{id}")
    public ObjectActionResult<ProjectDto> get(@PathVariable("id") Long id) {
        return ActionUtil.objectResult(projectService.get(id));
    }

    @GetMapping(path = "/getByName/{name}")
    public ObjectActionResult<ProjectDto> getByName(@PathVariable("name") String name) {
        return ActionUtil.objectResult(projectService.get(name));
    }

    @PostMapping(path = "/page")
    public PageActionResult<ProjectDto> page(
            @RequestBody @Validated({FindValidationGroup.class, PagingValidationGroup.class}) ProjectParamVo param) {
        Page<ProjectDto> result = projectService.findPage(param);
        return ActionUtil.pageResult(result);
    }

    @PostMapping(path = "/add")
    public ObjectActionResult<Long> add(@RequestBody @Validated({CreateValidationGroup.class}) ProjectDto param) {
        return ActionUtil.objectResult(projectService.add(param));
    }

    @PostMapping(path = "/update")
    public ActionResult update(@RequestBody @Validated({UpdateValidationGroup.class}) ProjectDto param) {
        projectService.update(param);
        return ActionUtil.success();
    }

    @PostMapping(path = "/delete/{id}")
    public ActionResult delete(@PathVariable("id") Long id) {
        projectService.delete(id);
        return ActionUtil.success();
    }
}
